<?php
/*	Created by Alexander Alexandrov aka SS
			hazg@mail.ru				*/
define('SSLIB_Image', TRUE);

class ErrImage extends Err
{
    
	function __construct($message, $code = 0)
	{
		header("Content-type: image/jpeg");
		parent::__construct($message, $code);
		die($this->__toString());
	}
	
	function __toString()
	{
		$convert = Image::findMagick('convert');
		$file = basename($this->file)."($this->line)";
		$msg = $this->message.'\n'.$file;
		
		$tmp = Sslib::SiteRoot('/tmp/'.RandomKey().'.jpg');
		$q = "$convert -background gray -fill white -pointsize 13 -bordercolor darkgray  -border 3 -gravity northwest label:\"$msg\" $tmp";
		$ex = exec($q, $err, $out) ;
		$ret = file_get_contents($tmp);
		unlink($tmp);
		return $ret;
	}
}


class Image
{
    static function Captcha($filename, $text, $color, $font, $fontSize = 50)
    {
        // /home/ss/http/mams-club.ru/private/fonts/KBChopinScript.pfb
        $convert = Image::findMagick('convert');
        //$filename = RandomKey('16');
        //$filename = 'tmp/captcha.jpg';
        //print $font;
        //$font = '/home/ss/http/mams-club.ru/private/fonts/KBChopinScript.pfb';
        //$w1 = rand(2,6);
        $w1 = 1;
        $w2 = rand(40, 80);
        $arc = rand(1, 260);
        if(file_exists($filename))    unlink($filename);
//        
         /*$q = $convert.' -font '.$font.' \
          -pointsize '.$fontSize.' \
          -background \'#fffab8\' \
          -fill \'#FF5E00\' \
          -gravity NorthWest \
          -fill \'#fffab8\' -annotate +0+0 \''.$text.'\' -stroke black -strokewidth 1 \
          label:"'.$text.'" \
          -virtual-pixel background \
          -distort Arc '.$arc.' \
          -trim \
          '.$filename;
         */
        //-distort Arc 230
        $q = $convert." -gravity NorthWest -trim  -stroke black -strokewidth .5 -fill \\$color -virtual-pixel background ".
"-wave {$w1}x$w2 -font $font  -background \\$color -pointsize $fontSize  ".
"label:\"$text\" ".$filename;
/*$q = $convert." -gravity NorthWest -trim  -stroke black -strokewidth .5 -virtual-pixel background ".
"-wave {$w1}x$w2 -font $font -pointsize $fontSize  ".
"label:\"$text\" ".$filename;
  */      
         //-wave '.$w1.'x'.$w2.' \
         
         exec($q, $err);
		 /*){return print_r($err, true);}*/
         
         
         //-fill black  -annotate +0+0 \''.$text.'\'  -motion-blur 0x40-10 \
         //-distort Arc 90 \
         //          -rotate 12 \
         
         //
        /*$q =  "convert -size 320x100 xc:lightblue -font $font -pointsize 72 -fill white \
           -stroke black -strokewidth 25 -annotate +25+65 'Любовь' \
           -stroke white -strokewidth 20 -annotate +25+65 'Любовь' \
           -stroke black -strokewidth 15 -annotate +25+65 'Любовь' \
           -stroke white -strokewidth 10 -annotate +25+65 'Любовь' \
           -stroke black -strokewidth  5 -annotate +25+65 'Любовь' \
           -stroke none                  -annotate +25+65 'Любовь' \
           $filename";*/
        //-background 
        
        /*$q = $convert." -trim -virtual-pixel background -font $font -pointsize 36 \
              -distort Arc 360
              -label 'Порно' \
              -gravity NorthWest \
              -fill red \
              $filename
        ";*/
//            -shade 1x20 \  -colorize red \
              //print $convert.' '.$filename.' +level-colors Navy, tmp/2.png';
        //exec($convert.' '.$filename.' +level-colors Navy, tmp/2.png');
              
        /*print $q;
        if(!exec($q
            , $err))
            {
                return print_r($err, true);
            }*/
        //Text::Crypt($text);
        //print $filename;
    }
	static function Size($path)
	{
		 $identify = Image::findMagick('identify');
		 //print "$identify $path, $out, $err";
		 $out = null;
		 $err = null;
		 //print "$identify $path";
		 if(!file_exists($path)) return false;
		 if(!exec("$identify $path", $out, $err))
		 {
		 	//new Err("Image::Size $err $out");
		 	return false;
		 	//print_r("$err ");
		 	//print_r($out);
		 }
		 if(!preg_match('@(\d+)x(\d+)@', $out[0], $matches))
		 	return false;
		 return array('width'=>$matches[1],'height'=>$matches[2]);
	}
	private static $magickpaths = array('/usr/local/bin/','/usr/bin/');
	static function Convert($path, $outpath, $totype = "jpg")
	{
		return true;
	}
	/**
	 * @param path
	 * @param outpath
	 * 
	 * {%} {@} {!} {<} {>} {^}
	 */
	static function Thumb($path,$outpath,$new_w,$new_h, $resize_type = "")
	{
		$convert = Image::findMagick('convert');
		
		system("$convert -strip $path -resize {$new_w}x{$new_h}$resize_type $outpath", $out);
		
		return Image::Size($outpath);
	}
	static function Watermark($src, $wtm, $dest = "", $gravity = "center")
	{
		if(empty($dest)) $dest = $src;
		$composite = Image::findMagick('composite');		
		exec("$composite -gravity $gravity $wtm $src $dest", $arr);
	}
	static function findMagick($prog)
	{
		$path = Sslib::Variable('imagemagick.path');
		if(!$path)
			foreach(Image::$magickpaths as $path){if(file_exists($path.$prog)){return $path.$prog;}}
		else 
			return $path.$prog;
		return FALSE;
	}
}
?>